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); }
public ConfigurationDescriptor GetConfigurationDescriptor() { // Todo: more than just the first configuration. return(ConfigurationDescriptor.Parse(GetDescriptor(DescriptorType.Configuration, 0))); }