private USB.ConfigurationDescriptor ReadConfigurationDescriptor(IUSBDevice device, byte configurationId, out USB.InterfaceDescriptor[] interfaceDescriptors) { var setupPacket = new SetupPacket { Recipient = PacketRecipient.Device, Type = PacketType.Standard, Direction = Direction.DeviceToHost, Request = (byte)StandardRequest.GetDescriptor, Value = (ushort)(((int)DescriptorType.Configuration << 8) | configurationId), Count = (ushort)Packet.CalculateLength <USB.ConfigurationDescriptor>() }; // first ask for the configuration descriptor non-recursively ... var configurationDescriptorBytes = HandleSetupPacketSync(device, setupPacket); var result = Packet.Decode <USB.ConfigurationDescriptor>(configurationDescriptorBytes); interfaceDescriptors = new USB.InterfaceDescriptor[result.NumberOfInterfaces]; // ... read the total length of a recursive structure ... setupPacket.Count = result.TotalLength; // ... and only then read the whole structure again. var recursiveBytes = HandleSetupPacketSync(device, setupPacket); var currentOffset = Packet.CalculateLength <USB.ConfigurationDescriptor>(); for (var i = 0; i < interfaceDescriptors.Length; i++) { interfaceDescriptors[i] = Packet.Decode <USB.InterfaceDescriptor>(recursiveBytes, currentOffset); // skip next interface descriptor with associated endpoint descriptors (each of size 7) currentOffset += Packet.CalculateLength <USBIP.InterfaceDescriptor>() + interfaceDescriptors[i].NumberOfEndpoints * 7; } return(result); }
private USB.ConfigurationDescriptor ReadConfigurationDescriptor(IUSBDevice device, byte configurationId, out USB.InterfaceDescriptor[] interfaceDescriptors) { var setupPacket = new SetupPacket { Recipient = PacketRecipient.Device, Type = PacketType.Standard, Direction = Direction.DeviceToHost, Request = (byte)StandardRequest.GetDescriptor, Value = (ushort)(((int)DescriptorType.Configuration << 8) | configurationId), Count = (ushort)Packet.CalculateLength <USB.ConfigurationDescriptor>() }; // first ask for the configuration descriptor non-recursively ... var configurationDescriptorBytes = HandleSetupPacketSync(device, setupPacket); var result = Packet.Decode <USB.ConfigurationDescriptor>(configurationDescriptorBytes); interfaceDescriptors = new USB.InterfaceDescriptor[result.NumberOfInterfaces]; // ... read the total length of a recursive structure ... setupPacket.Count = result.TotalLength; // ... and only then read the whole structure again. var recursiveBytes = HandleSetupPacketSync(device, setupPacket); var currentOffset = Packet.CalculateLength <USB.ConfigurationDescriptor>(); for (var i = 0; i < interfaceDescriptors.Length; i++) { interfaceDescriptors[i] = Packet.Decode <USB.InterfaceDescriptor>(recursiveBytes, currentOffset); if (i != interfaceDescriptors.Length - 1) { // skip until the next interface descriptor currentOffset += Packet.CalculateLength <USB.InterfaceDescriptor>(); // the second byte of each descriptor contains the type while (recursiveBytes[currentOffset + 1] != (byte)DescriptorType.Interface) { // the first byte of each descriptor contains the length in bytes currentOffset += recursiveBytes[currentOffset]; } } } return(result); }