Mercurial > hg > ucis.core
comparison 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 |
comparison
equal
deleted
inserted
replaced
46:053cc617af54 | 47:15ddb1e0e2a5 |
---|---|
27 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | 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"); | 28 if (buffer[offset + 1] != (Byte)UsbDescriptorType.String) throw new InvalidOperationException("The descriptor is not a string descriptor"); |
29 int slen = buffer[offset]; | 29 int slen = buffer[offset]; |
30 if (slen > length) throw new InvalidOperationException("The string has been truncated"); | 30 if (slen > length) throw new InvalidOperationException("The string has been truncated"); |
31 return Encoding.Unicode.GetString(buffer, offset + 2, slen - 2); | 31 return Encoding.Unicode.GetString(buffer, offset + 2, slen - 2); |
32 } | |
33 public static String GetStringFromDevice(IUsbInterface device, byte index, short langId) { | |
34 Byte[] buff = new Byte[256]; | |
35 int len = device.GetDescriptor((Byte)UsbDescriptorType.String, index, langId, buff, 0, buff.Length); | |
36 return GetString(buff, 0, len); | |
32 } | 37 } |
33 } | 38 } |
34 [StructLayout(LayoutKind.Sequential, Pack = 1)] | 39 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
35 public struct UsbDeviceDescriptor { | 40 public struct UsbDeviceDescriptor { |
36 byte bmLength; | 41 byte bmLength; |
64 public unsafe static UsbDeviceDescriptor FromByteArray(Byte[] buffer, int offset, int length) { | 69 public unsafe static UsbDeviceDescriptor FromByteArray(Byte[] buffer, int offset, int length) { |
65 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); | 70 if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length"); |
66 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); | 71 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions"); |
67 fixed (Byte* ptr = buffer) return *(UsbDeviceDescriptor*)(ptr + offset); | 72 fixed (Byte* ptr = buffer) return *(UsbDeviceDescriptor*)(ptr + offset); |
68 } | 73 } |
74 public static UsbDeviceDescriptor FromDevice(IUsbInterface device) { | |
75 Byte[] buff = new Byte[UsbDeviceDescriptor.Size]; | |
76 int len = device.GetDescriptor((Byte)UsbDescriptorType.Device, 0, 0, buff, 0, buff.Length); | |
77 return FromByteArray(buff, 0, len); | |
78 } | |
69 public static unsafe int Size { get { return sizeof(UsbDeviceDescriptor); } } | 79 public static unsafe int Size { get { return sizeof(UsbDeviceDescriptor); } } |
70 } | 80 } |
71 [StructLayout(LayoutKind.Sequential, Pack = 1)] | 81 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
72 public struct UsbConfigurationDescriptor { | 82 public struct UsbConfigurationDescriptor { |
73 byte bmLength; | 83 byte bmLength; |