Mercurial > hg > ucis.core
annotate USBLib/Descriptor/UsbDescriptor.cs @ 65:abe0d55a2201
Removed some redundant USB communication code
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 15 Oct 2013 16:19:45 +0200 |
parents | 99ed461509fe |
children | e811297f5aa4 |
rev | line source |
---|---|
21 | 1 ???using System; |
22 | 2 using System.Runtime.InteropServices; |
21 | 3 using System.Text; |
4 using UCIS.USBLib.Communication; | |
5 | |
6 namespace UCIS.USBLib.Descriptor { | |
7 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
8 public struct UsbDescriptor { | |
9 byte bmLength; | |
10 byte bType; | |
11 public Byte Length { get { return bmLength; } } | |
12 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
13 internal static short FromLittleEndian(short value) { | |
14 if (BitConverter.IsLittleEndian) return value; | |
15 return (short)(((value & 0xFF) << 8) | ((value >> 8) & 0xFF)); | |
16 } | |
17 public unsafe static UsbDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
18 if (length < Marshal.SizeOf(typeof(UsbDescriptor))) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
19 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
20 fixed (Byte* ptr = buffer) return *(UsbDescriptor*)(ptr + offset); | |
21 } | |
22 } | |
23 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
24 public struct UsbStringDescriptor { | |
25 public static String GetString(Byte[] buffer, int offset, int length) { | |
26 if (length < 2) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
27 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
28 if (buffer[offset + 1] != (Byte)UsbDescriptorType.String) throw new InvalidOperationException("The descriptor is not a string descriptor"); | |
29 int slen = buffer[offset]; | |
30 if (slen > length) throw new InvalidOperationException("The string has been truncated"); | |
31 return Encoding.Unicode.GetString(buffer, offset + 2, slen - 2); | |
32 } | |
47
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
33 public static String GetStringFromDevice(IUsbInterface device, byte index, short langId) { |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
34 Byte[] buff = new Byte[256]; |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
35 int len = device.GetDescriptor((Byte)UsbDescriptorType.String, index, langId, buff, 0, buff.Length); |
58
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
47
diff
changeset
|
36 if (len == 0) return null; |
47
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
37 return GetString(buff, 0, len); |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
38 } |
21 | 39 } |
40 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
41 public struct UsbDeviceDescriptor { | |
42 byte bmLength; | |
43 byte bType; | |
44 short bcdUSB; | |
45 byte bDeviceClass; | |
46 byte bDeviceSubClass; | |
47 byte bDeviceProtocol; | |
48 byte bMaxControlPacketSize; | |
49 short idVendor; | |
50 short idProduct; | |
51 short bcdDevice; | |
52 byte iManufacturer; | |
53 byte iProduct; | |
54 byte iSerialNumber; | |
55 byte numConfigurations; | |
56 public Byte Length { get { return bmLength; } } | |
57 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; }} | |
58 public short USBVersion { get { return UsbDescriptor.FromLittleEndian(bcdUSB); } } | |
59 public Byte DeviceClass { get { return bDeviceClass; } } | |
60 public Byte DeviceSubClass { get { return bDeviceSubClass; } } | |
61 public Byte DeviceProtocol { get { return bDeviceProtocol; } } | |
64
99ed461509fe
Fixed data type for USB vendor and product IDs
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
62 public UInt16 DeviceVersion { get { return (UInt16)UsbDescriptor.FromLittleEndian(bcdDevice); } } |
21 | 63 public Byte MaxControlPacketSize { get { return bMaxControlPacketSize; } } |
64
99ed461509fe
Fixed data type for USB vendor and product IDs
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
64 public UInt16 VendorID { get { return (UInt16)UsbDescriptor.FromLittleEndian(idVendor); } } |
99ed461509fe
Fixed data type for USB vendor and product IDs
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
65 public UInt16 ProductID { get { return (UInt16)UsbDescriptor.FromLittleEndian(idProduct); } } |
21 | 66 public Byte ManufacturerStringID { get { return iManufacturer; } } |
67 public Byte ProductStringID { get { return iProduct; } } | |
68 public Byte SerialNumberStringID { get { return iSerialNumber; } } | |
69 public Byte NumConfigurations { get { return numConfigurations; } } | |
70 public unsafe static UsbDeviceDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
71 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
72 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
73 fixed (Byte* ptr = buffer) return *(UsbDeviceDescriptor*)(ptr + offset); | |
74 } | |
47
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
75 public static UsbDeviceDescriptor FromDevice(IUsbInterface device) { |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
76 Byte[] buff = new Byte[UsbDeviceDescriptor.Size]; |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
77 int len = device.GetDescriptor((Byte)UsbDescriptorType.Device, 0, 0, buff, 0, buff.Length); |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
78 return FromByteArray(buff, 0, len); |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
79 } |
21 | 80 public static unsafe int Size { get { return sizeof(UsbDeviceDescriptor); } } |
81 } | |
82 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
83 public struct UsbConfigurationDescriptor { | |
84 byte bmLength; | |
85 byte bType; | |
86 short wTotalLength; | |
87 byte bNumInterfaces; | |
88 byte bConfigurationValue; | |
89 byte bConfigurationStringID; | |
90 byte bmAttributes; | |
91 byte bMaxPower; | |
92 public Byte Length { get { return bmLength; } } | |
93 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
94 public short TotalLength { get { return UsbDescriptor.FromLittleEndian(wTotalLength); } } | |
95 public Byte NumInterfaces { get { return bNumInterfaces; } } | |
96 public Byte ConfigurationValue { get { return bConfigurationValue; } } | |
97 public Byte ConfigurationStringID { get { return bConfigurationStringID; } } | |
98 public Boolean SelfPowered { get { return 0 != (bmAttributes & (1 << 6)); } } | |
99 public Boolean RemoteWakeup { get { return 0 != (bmAttributes & (1 << 5)); } } | |
100 public int MaxPowerMA { get { return bMaxPower * 2; } } | |
101 public unsafe static UsbConfigurationDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
102 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
103 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
104 fixed (Byte* ptr = buffer) return *(UsbConfigurationDescriptor*)(ptr + offset); | |
105 } | |
65
abe0d55a2201
Removed some redundant USB communication code
Ivo Smits <Ivo@UCIS.nl>
parents:
64
diff
changeset
|
106 public static UsbConfigurationDescriptor FromDevice(IUsbInterface device, Byte index) { |
abe0d55a2201
Removed some redundant USB communication code
Ivo Smits <Ivo@UCIS.nl>
parents:
64
diff
changeset
|
107 Byte[] buff = new Byte[UsbConfigurationDescriptor.Size]; |
abe0d55a2201
Removed some redundant USB communication code
Ivo Smits <Ivo@UCIS.nl>
parents:
64
diff
changeset
|
108 int len = device.GetDescriptor((Byte)UsbDescriptorType.Configuration, index, 0, buff, 0, buff.Length); |
abe0d55a2201
Removed some redundant USB communication code
Ivo Smits <Ivo@UCIS.nl>
parents:
64
diff
changeset
|
109 return FromByteArray(buff, 0, len); |
abe0d55a2201
Removed some redundant USB communication code
Ivo Smits <Ivo@UCIS.nl>
parents:
64
diff
changeset
|
110 } |
21 | 111 public static unsafe int Size { get { return sizeof(UsbConfigurationDescriptor); } } |
112 } | |
113 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
114 public struct UsbInterfaceDescriptor { | |
115 byte bmLength; | |
116 byte bType; | |
117 byte bInterfaceNumber; | |
118 byte bAlternateSetting; | |
119 byte bNumEndpoints; | |
120 byte bInterfaceClass; | |
121 byte bInterfaceSubClass; | |
122 byte bInterfaceProtocol; | |
123 byte bInterfaceStringID; | |
124 public Byte Length { get { return bmLength; } } | |
125 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
126 public Byte InterfaceNumber { get { return bInterfaceNumber; } } | |
127 public Byte AlternateSetting { get { return bAlternateSetting; } } | |
128 public Byte NumEndpoints { get { return bNumEndpoints; } } | |
129 public Byte InterfaceClass { get { return bInterfaceClass; } } | |
130 public Byte InterfaceSubClass { get { return bInterfaceSubClass; } } | |
131 public Byte InterfaceProtocol { get { return bInterfaceProtocol; } } | |
132 public Byte InterfaceStringID { get { return bInterfaceStringID; } } | |
133 public unsafe static UsbInterfaceDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
134 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
135 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
136 fixed (Byte* ptr = buffer) return *(UsbInterfaceDescriptor*)(ptr + offset); | |
137 } | |
138 public static unsafe int Size { get { return sizeof(UsbInterfaceDescriptor); } } | |
139 } | |
140 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
141 public struct UsbEndpointDescriptor { | |
142 byte bmLength; | |
143 byte bType; | |
144 byte bEndpointAddress; | |
145 Byte bmAttributes; | |
146 short wMaxPacketSize; | |
147 byte bInterval; | |
148 public Byte Length { get { return bmLength; } } | |
149 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
150 public Byte EndpointAddress { get { return bEndpointAddress; } } | |
151 public Boolean IsInput { get { return 0 != (EndpointAddress & (1 << 7)); } } | |
152 public int EndpointNumber { get { return EndpointAddress & 0xF; } } | |
153 public int TransferType { get { return bmAttributes & 3; } } | |
154 public Boolean IsControl { get { return TransferType == 0; } } | |
155 public Boolean IsIsochronous { get { return TransferType == 1; } } | |
156 public Boolean IsBulk { get { return TransferType == 2; } } | |
157 public Boolean IsInterrupt { get { return TransferType == 3; } } | |
158 public int MaxPacketSize { get { return UsbDescriptor.FromLittleEndian(wMaxPacketSize) & 0x7FF; } } | |
159 public Byte Interval { get { return bInterval; } } | |
160 public unsafe static UsbEndpointDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
161 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
162 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
163 fixed (Byte* ptr = buffer) return *(UsbEndpointDescriptor*)(ptr + offset); | |
164 } | |
165 public static unsafe int Size { get { return sizeof(UsbEndpointDescriptor); } } | |
166 } | |
167 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
168 public struct UsbHIDDescriptor { | |
169 byte bmLength; | |
170 byte bType; | |
171 short bcdHID; | |
172 byte bCountryCode; | |
173 byte bNumDescriptors; | |
174 byte bDescriptorType; | |
175 short wDescriptorLength; | |
176 public Byte Length { get { return bmLength; } } | |
177 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
178 public short HIDVersion { get { return UsbDescriptor.FromLittleEndian(bcdHID); } } | |
179 public Byte CountryCode { get { return bCountryCode; } } | |
180 public Byte NumDescriptors { get { return bNumDescriptors; } } | |
181 public UsbDescriptorType DescriptorType { get { return (UsbDescriptorType)bDescriptorType; } } | |
182 public short DescriptorLength { get { return UsbDescriptor.FromLittleEndian(wDescriptorLength); } } | |
183 public unsafe static UsbHIDDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
184 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
185 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
186 fixed (Byte* ptr = buffer) return *(UsbHIDDescriptor*)(ptr + offset); | |
187 } | |
188 public static unsafe int Size { get { return sizeof(UsbHIDDescriptor); } } | |
189 } | |
190 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
191 public struct UsbHubDescriptor { | |
192 byte bmLength; | |
193 byte bType; | |
194 byte bNumPorts; | |
195 short wHubCharacteristics; | |
196 byte bPwrOn2PwrGood; //2ms intervals | |
197 byte bHubControllerCurrent; | |
198 public Byte Length { get { return bmLength; } } | |
199 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
200 public Byte NumPorts { get { return bNumPorts; } } | |
201 public Boolean IsCompoundDevice { get { return 0 != (wHubCharacteristics & (1 << 2)); } } | |
202 public Byte HubControllerCurrent { get { return bHubControllerCurrent; } } | |
203 public unsafe static UsbHubDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
204 if (length < Marshal.SizeOf(typeof(UsbHubDescriptor))) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
205 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
206 fixed (Byte* ptr = buffer) return *(UsbHubDescriptor*)(ptr + offset); | |
207 } | |
58
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
47
diff
changeset
|
208 public static unsafe int Size { get { return sizeof(UsbHubDescriptor); } } |
21 | 209 } |
210 } |