changeset 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 053cc617af54
children f553f6e0a396 556b4fb511bd
files USBLib/Descriptor/UsbDescriptor.cs
diffstat 1 files changed, 10 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/USBLib/Descriptor/UsbDescriptor.cs	Sun Jun 30 16:28:36 2013 +0200
+++ b/USBLib/Descriptor/UsbDescriptor.cs	Tue Jul 16 13:16:04 2013 +0200
@@ -30,6 +30,11 @@
 			if (slen > length) throw new InvalidOperationException("The string has been truncated");
 			return Encoding.Unicode.GetString(buffer, offset + 2, slen - 2);
 		}
+		public static String GetStringFromDevice(IUsbInterface device, byte index, short langId) {
+			Byte[] buff = new Byte[256];
+			int len = device.GetDescriptor((Byte)UsbDescriptorType.String, index, langId, buff, 0, buff.Length);
+			return GetString(buff, 0, len);
+		}
 	}
 	[StructLayout(LayoutKind.Sequential, Pack = 1)]
 	public struct UsbDeviceDescriptor {
@@ -66,6 +71,11 @@
 			if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer dimensions");
 			fixed (Byte* ptr = buffer) return *(UsbDeviceDescriptor*)(ptr + offset);
 		}
+		public static UsbDeviceDescriptor FromDevice(IUsbInterface device) {
+			Byte[] buff = new Byte[UsbDeviceDescriptor.Size];
+			int len = device.GetDescriptor((Byte)UsbDescriptorType.Device, 0, 0, buff, 0, buff.Length);
+			return FromByteArray(buff, 0, len);
+		}
 		public static unsafe int Size { get { return sizeof(UsbDeviceDescriptor); } }
 	}
 	[StructLayout(LayoutKind.Sequential, Pack = 1)]