Mercurial > hg > ucis.core
annotate USBLib/Descriptor/UsbDescriptor.cs @ 47:15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 16 Jul 2013 13:16:04 +0200 |
parents | 5b14fed54a89 |
children | fd63c453ff65 |
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); |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
36 return GetString(buff, 0, len); |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
37 } |
21 | 38 } |
39 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
40 public struct UsbDeviceDescriptor { | |
41 byte bmLength; | |
42 byte bType; | |
43 short bcdUSB; | |
44 byte bDeviceClass; | |
45 byte bDeviceSubClass; | |
46 byte bDeviceProtocol; | |
47 byte bMaxControlPacketSize; | |
48 short idVendor; | |
49 short idProduct; | |
50 short bcdDevice; | |
51 byte iManufacturer; | |
52 byte iProduct; | |
53 byte iSerialNumber; | |
54 byte numConfigurations; | |
55 public Byte Length { get { return bmLength; } } | |
56 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; }} | |
57 public short USBVersion { get { return UsbDescriptor.FromLittleEndian(bcdUSB); } } | |
58 public Byte DeviceClass { get { return bDeviceClass; } } | |
59 public Byte DeviceSubClass { get { return bDeviceSubClass; } } | |
60 public Byte DeviceProtocol { get { return bDeviceProtocol; } } | |
61 public short DeviceVersion { get { return UsbDescriptor.FromLittleEndian(bcdDevice); } } | |
62 public Byte MaxControlPacketSize { get { return bMaxControlPacketSize; } } | |
63 public short VendorID { get { return UsbDescriptor.FromLittleEndian(idVendor); } } | |
64 public short ProductID { get { return UsbDescriptor.FromLittleEndian(idProduct); } } | |
65 public Byte ManufacturerStringID { get { return iManufacturer; } } | |
66 public Byte ProductStringID { get { return iProduct; } } | |
67 public Byte SerialNumberStringID { get { return iSerialNumber; } } | |
68 public Byte NumConfigurations { get { return numConfigurations; } } | |
69 public unsafe static UsbDeviceDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
70 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
71 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
72 fixed (Byte* ptr = buffer) return *(UsbDeviceDescriptor*)(ptr + offset); | |
73 } | |
47
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 return FromByteArray(buff, 0, len); |
15ddb1e0e2a5
USBLib: added convenience functions to retrieve descriptors from devices
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
78 } |
21 | 79 public static unsafe int Size { get { return sizeof(UsbDeviceDescriptor); } } |
80 } | |
81 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
82 public struct UsbConfigurationDescriptor { | |
83 byte bmLength; | |
84 byte bType; | |
85 short wTotalLength; | |
86 byte bNumInterfaces; | |
87 byte bConfigurationValue; | |
88 byte bConfigurationStringID; | |
89 byte bmAttributes; | |
90 byte bMaxPower; | |
91 public Byte Length { get { return bmLength; } } | |
92 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
93 public short TotalLength { get { return UsbDescriptor.FromLittleEndian(wTotalLength); } } | |
94 public Byte NumInterfaces { get { return bNumInterfaces; } } | |
95 public Byte ConfigurationValue { get { return bConfigurationValue; } } | |
96 public Byte ConfigurationStringID { get { return bConfigurationStringID; } } | |
97 public Boolean SelfPowered { get { return 0 != (bmAttributes & (1 << 6)); } } | |
98 public Boolean RemoteWakeup { get { return 0 != (bmAttributes & (1 << 5)); } } | |
99 public int MaxPowerMA { get { return bMaxPower * 2; } } | |
100 public unsafe static UsbConfigurationDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
101 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
102 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
103 fixed (Byte* ptr = buffer) return *(UsbConfigurationDescriptor*)(ptr + offset); | |
104 } | |
105 public static unsafe int Size { get { return sizeof(UsbConfigurationDescriptor); } } | |
106 } | |
107 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
108 public struct UsbInterfaceDescriptor { | |
109 byte bmLength; | |
110 byte bType; | |
111 byte bInterfaceNumber; | |
112 byte bAlternateSetting; | |
113 byte bNumEndpoints; | |
114 byte bInterfaceClass; | |
115 byte bInterfaceSubClass; | |
116 byte bInterfaceProtocol; | |
117 byte bInterfaceStringID; | |
118 public Byte Length { get { return bmLength; } } | |
119 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
120 public Byte InterfaceNumber { get { return bInterfaceNumber; } } | |
121 public Byte AlternateSetting { get { return bAlternateSetting; } } | |
122 public Byte NumEndpoints { get { return bNumEndpoints; } } | |
123 public Byte InterfaceClass { get { return bInterfaceClass; } } | |
124 public Byte InterfaceSubClass { get { return bInterfaceSubClass; } } | |
125 public Byte InterfaceProtocol { get { return bInterfaceProtocol; } } | |
126 public Byte InterfaceStringID { get { return bInterfaceStringID; } } | |
127 public unsafe static UsbInterfaceDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
128 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
129 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
130 fixed (Byte* ptr = buffer) return *(UsbInterfaceDescriptor*)(ptr + offset); | |
131 } | |
132 public static unsafe int Size { get { return sizeof(UsbInterfaceDescriptor); } } | |
133 } | |
134 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
135 public struct UsbEndpointDescriptor { | |
136 byte bmLength; | |
137 byte bType; | |
138 byte bEndpointAddress; | |
139 Byte bmAttributes; | |
140 short wMaxPacketSize; | |
141 byte bInterval; | |
142 public Byte Length { get { return bmLength; } } | |
143 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
144 public Byte EndpointAddress { get { return bEndpointAddress; } } | |
145 public Boolean IsInput { get { return 0 != (EndpointAddress & (1 << 7)); } } | |
146 public int EndpointNumber { get { return EndpointAddress & 0xF; } } | |
147 public int TransferType { get { return bmAttributes & 3; } } | |
148 public Boolean IsControl { get { return TransferType == 0; } } | |
149 public Boolean IsIsochronous { get { return TransferType == 1; } } | |
150 public Boolean IsBulk { get { return TransferType == 2; } } | |
151 public Boolean IsInterrupt { get { return TransferType == 3; } } | |
152 public int MaxPacketSize { get { return UsbDescriptor.FromLittleEndian(wMaxPacketSize) & 0x7FF; } } | |
153 public Byte Interval { get { return bInterval; } } | |
154 public unsafe static UsbEndpointDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
155 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
156 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
157 fixed (Byte* ptr = buffer) return *(UsbEndpointDescriptor*)(ptr + offset); | |
158 } | |
159 public static unsafe int Size { get { return sizeof(UsbEndpointDescriptor); } } | |
160 } | |
161 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
162 public struct UsbHIDDescriptor { | |
163 byte bmLength; | |
164 byte bType; | |
165 short bcdHID; | |
166 byte bCountryCode; | |
167 byte bNumDescriptors; | |
168 byte bDescriptorType; | |
169 short wDescriptorLength; | |
170 public Byte Length { get { return bmLength; } } | |
171 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
172 public short HIDVersion { get { return UsbDescriptor.FromLittleEndian(bcdHID); } } | |
173 public Byte CountryCode { get { return bCountryCode; } } | |
174 public Byte NumDescriptors { get { return bNumDescriptors; } } | |
175 public UsbDescriptorType DescriptorType { get { return (UsbDescriptorType)bDescriptorType; } } | |
176 public short DescriptorLength { get { return UsbDescriptor.FromLittleEndian(wDescriptorLength); } } | |
177 public unsafe static UsbHIDDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
178 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
179 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
180 fixed (Byte* ptr = buffer) return *(UsbHIDDescriptor*)(ptr + offset); | |
181 } | |
182 public static unsafe int Size { get { return sizeof(UsbHIDDescriptor); } } | |
183 } | |
184 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
185 public struct UsbHubDescriptor { | |
186 byte bmLength; | |
187 byte bType; | |
188 byte bNumPorts; | |
189 short wHubCharacteristics; | |
190 byte bPwrOn2PwrGood; //2ms intervals | |
191 byte bHubControllerCurrent; | |
192 public Byte Length { get { return bmLength; } } | |
193 public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; } } | |
194 public Byte NumPorts { get { return bNumPorts; } } | |
195 public Boolean IsCompoundDevice { get { return 0 != (wHubCharacteristics & (1 << 2)); } } | |
196 public Byte HubControllerCurrent { get { return bHubControllerCurrent; } } | |
197 public unsafe static UsbHubDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | |
198 if (length < Marshal.SizeOf(typeof(UsbHubDescriptor))) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | |
199 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | |
200 fixed (Byte* ptr = buffer) return *(UsbHubDescriptor*)(ptr + offset); | |
201 } | |
202 } | |
203 } |