diff USBLib/Descriptor/UsbDescriptor.cs @ 68:e811297f5aa4

Updated USBLib: removed old LibUsbDotNet compatibility code and added new information helper classes
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 16 Oct 2013 16:58:39 +0200
parents abe0d55a2201
children 94df2951d118
line wrap: on
line diff
--- a/USBLib/Descriptor/UsbDescriptor.cs	Wed Oct 16 01:11:49 2013 +0200
+++ b/USBLib/Descriptor/UsbDescriptor.cs	Wed Oct 16 16:58:39 2013 +0200
@@ -2,8 +2,25 @@
 using System.Runtime.InteropServices;
 using System.Text;
 using UCIS.USBLib.Communication;
+using UCIS.Util;
 
 namespace UCIS.USBLib.Descriptor {
+	public struct UsbDescriptorBlob {
+		public UsbDescriptorBlob(ArraySegment<Byte> data) : this() { this.Data = data; }
+		public UsbDescriptorBlob(Byte[] data) : this() { this.Data = new ArraySegment<byte>(data); }
+		public UsbDescriptorBlob(Byte[] data, int offset) : this() { this.Data = new ArraySegment<byte>(data, offset, data[offset + 0]); }
+		public ArraySegment<Byte> Data { get; private set; }
+		public Byte Length { get { return Data.Array[Data.Offset + 0]; } }
+		public UsbDescriptorType Type { get { return (UsbDescriptorType)Data.Array[Data.Offset + 1]; } }
+		public Byte[] GetBytes() { return ArrayUtil.ToArray(Data); }
+		public static explicit operator UsbDescriptor(UsbDescriptorBlob self) { return UsbDescriptor.FromByteArray(self.Data.Array, self.Data.Offset, self.Data.Count); }
+		public static explicit operator UsbDeviceDescriptor(UsbDescriptorBlob self) { return UsbDeviceDescriptor.FromByteArray(self.Data.Array, self.Data.Offset, self.Data.Count); }
+		public static explicit operator UsbConfigurationDescriptor(UsbDescriptorBlob self) { return UsbConfigurationDescriptor.FromByteArray(self.Data.Array, self.Data.Offset, self.Data.Count); }
+		public static explicit operator UsbInterfaceDescriptor(UsbDescriptorBlob self) { return UsbInterfaceDescriptor.FromByteArray(self.Data.Array, self.Data.Offset, self.Data.Count); }
+		public static explicit operator UsbEndpointDescriptor(UsbDescriptorBlob self) { return UsbEndpointDescriptor.FromByteArray(self.Data.Array, self.Data.Offset, self.Data.Count); }
+		public static explicit operator UsbHidDescriptor(UsbDescriptorBlob self) { return UsbHidDescriptor.FromByteArray(self.Data.Array, self.Data.Offset, self.Data.Count); }
+		public static explicit operator UsbHubDescriptor(UsbDescriptorBlob self) { return UsbHubDescriptor.FromByteArray(self.Data.Array, self.Data.Offset, self.Data.Count); }
+	}
 	[StructLayout(LayoutKind.Sequential, Pack = 1)]
 	public struct UsbDescriptor {
 		byte bmLength;
@@ -56,7 +73,7 @@
 		public Byte Length { get { return bmLength; } }
 		public UsbDescriptorType Type { get { return (UsbDescriptorType)bType; }}
 		public short USBVersion { get { return UsbDescriptor.FromLittleEndian(bcdUSB); } }
-		public Byte DeviceClass { get { return bDeviceClass; } }
+		public UsbClassCode DeviceClass { get { return (UsbClassCode)bDeviceClass; } }
 		public Byte DeviceSubClass { get { return bDeviceSubClass; } }
 		public Byte DeviceProtocol { get { return bDeviceProtocol; } }
 		public UInt16 DeviceVersion { get { return (UInt16)UsbDescriptor.FromLittleEndian(bcdDevice); } }
@@ -67,14 +84,20 @@
 		public Byte ProductStringID { get { return iProduct; } }
 		public Byte SerialNumberStringID { get { return iSerialNumber; } }
 		public Byte NumConfigurations { get { return numConfigurations; } }
+		public unsafe Byte[] GetBytes() {
+			Byte[] buffer = new Byte[Size];
+			fixed (Byte* ptr = buffer) *(UsbDeviceDescriptor*)ptr = this;
+			return buffer;
+		}
 		public unsafe static UsbDeviceDescriptor FromByteArray(Byte[] buffer, int offset, int length) {
 			if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length");
 			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];
+			Byte[] buff = new Byte[Size];
 			int len = device.GetDescriptor((Byte)UsbDescriptorType.Device, 0, 0, buff, 0, buff.Length);
+			if (len == 0) return new UsbDeviceDescriptor();
 			return FromByteArray(buff, 0, len);
 		}
 		public static unsafe int Size { get { return sizeof(UsbDeviceDescriptor); } }
@@ -106,6 +129,7 @@
 		public static UsbConfigurationDescriptor FromDevice(IUsbInterface device, Byte index) {
 			Byte[] buff = new Byte[UsbConfigurationDescriptor.Size];
 			int len = device.GetDescriptor((Byte)UsbDescriptorType.Configuration, index, 0, buff, 0, buff.Length);
+			if (len == 0) return new UsbConfigurationDescriptor();
 			return FromByteArray(buff, 0, len);
 		}
 		public static unsafe int Size { get { return sizeof(UsbConfigurationDescriptor); } }
@@ -126,7 +150,7 @@
 		public Byte InterfaceNumber { get { return bInterfaceNumber; } }
 		public Byte AlternateSetting { get { return bAlternateSetting; } }
 		public Byte NumEndpoints { get { return bNumEndpoints; } }
-		public Byte InterfaceClass { get { return bInterfaceClass; } }
+		public UsbClassCode InterfaceClass { get { return (UsbClassCode)bInterfaceClass; } }
 		public Byte InterfaceSubClass { get { return bInterfaceSubClass; } }
 		public Byte InterfaceProtocol { get { return bInterfaceProtocol; } }
 		public Byte InterfaceStringID { get { return bInterfaceStringID; } }
@@ -165,7 +189,7 @@
 		public static unsafe int Size { get { return sizeof(UsbEndpointDescriptor); } }
 	}
 	[StructLayout(LayoutKind.Sequential, Pack = 1)]
-	public struct UsbHIDDescriptor {
+	public struct UsbHidDescriptor {
 		byte bmLength;
 		byte bType;
 		short bcdHID;
@@ -180,12 +204,12 @@
 		public Byte NumDescriptors { get { return bNumDescriptors; } }
 		public UsbDescriptorType DescriptorType { get { return (UsbDescriptorType)bDescriptorType; } }
 		public short DescriptorLength { get { return UsbDescriptor.FromLittleEndian(wDescriptorLength); } }
-		public unsafe static UsbHIDDescriptor FromByteArray(Byte[] buffer, int offset, int length) {
+		public unsafe static UsbHidDescriptor FromByteArray(Byte[] buffer, int offset, int length) {
 			if (length < Size) throw new ArgumentOutOfRangeException("length", "The data length is smaller than the descriptor length");
 			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 *(UsbHIDDescriptor*)(ptr + offset);
+			fixed (Byte* ptr = buffer) return *(UsbHidDescriptor*)(ptr + offset);
 		}
-		public static unsafe int Size { get { return sizeof(UsbHIDDescriptor); } }
+		public static unsafe int Size { get { return sizeof(UsbHidDescriptor); } }
 	}
 	[StructLayout(LayoutKind.Sequential, Pack = 1)]
 	public struct UsbHubDescriptor {