public static InterfaceDescriptor Parse(DescriptorNode n, List <DescriptorNode> subDescriptors) { if (n.Type != DescriptorType.Interface || n.Data.Length != 7) { throw new Exception("Invalid interface descriptor"); } InterfaceDescriptor i = new InterfaceDescriptor(); byte[] b = n.Data; i.Number = b[0]; i.AlternateSetting = b[1]; i.NumEndpoints = b[2]; i.Class = b[3]; i.SubClass = b[4]; i.Protocol = b[5]; i.DescriptionString = b[6]; i.RawSubDescriptors.AddRange(subDescriptors); return(i); }
public static ConfigurationDescriptor Parse(byte[] srcData) { ConfigurationDescriptor c = new ConfigurationDescriptor(); if (srcData.Length < 9 || srcData[0] != 9 || srcData[1] != (byte)DescriptorType.Configuration) { throw new Exception("Invalid configuration descriptor"); } int fullLength = srcData[2] | (srcData[3] << 8); if (fullLength < 9 || fullLength > srcData.Length) { throw new Exception("Invalid configuration descriptor length"); } c.NumInterfaces = srcData[4]; c.Index = srcData[5]; c.DescriptionString = srcData[6]; c.Attributes = (DeviceConfigurationFlags)srcData[7]; c.PowerDraw = srcData[8]; int cursor = 9; while (cursor < fullLength) { c.RawDescriptors.Add(new DescriptorNode(srcData, ref cursor)); } if (cursor != fullLength) { throw new Exception("Configuration descriptors overran specified length"); } // Next step, parse them off into interfaces. List <DescriptorNode> nodes = new List <DescriptorNode>(); DescriptorNode nodeInterface = null; foreach (DescriptorNode n in c.RawDescriptors) { if (n.Type == DescriptorType.Interface) { if (nodeInterface != null) { c.Interfaces.Add(InterfaceDescriptor.Parse(nodeInterface, nodes)); } nodes.Clear(); nodeInterface = n; } else { if (nodeInterface == null) { throw new Exception("Configuration descriptor has unknown descriptor blocks before the first interface."); } nodes.Add(n); } } if (nodeInterface != null) { c.Interfaces.Add(InterfaceDescriptor.Parse(nodeInterface, nodes)); } return(c); }