comparison USBLib/Communication/LibUsbDotNet.cs @ 61:2b24666cd759

Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 13 Oct 2013 00:48:28 +0200
parents 3424fa5a12c9
children abe0d55a2201
comparison
equal deleted inserted replaced
60:3424fa5a12c9 61:2b24666cd759
106 } 106 }
107 } 107 }
108 public IList<UsbConfigInfo> Configs { 108 public IList<UsbConfigInfo> Configs {
109 get { 109 get {
110 List<UsbConfigInfo> rtnConfigs = new List<UsbConfigInfo>(); 110 List<UsbConfigInfo> rtnConfigs = new List<UsbConfigInfo>();
111 byte[] cfgBuffer = new byte[UsbConstants.MAX_CONFIG_SIZE];
112 int iConfigs = Info.Descriptor.ConfigurationCount; 111 int iConfigs = Info.Descriptor.ConfigurationCount;
113 for (int iConfig = 0; iConfig < iConfigs; iConfig++) { 112 for (int iConfig = 0; iConfig < iConfigs; iConfig++) {
114 int iBytesTransmitted; 113 int iBytesTransmitted;
114 byte[] cfgBuffer = new byte[9];
115 if (!GetDescriptor((byte)UsbDescriptorType.Configuration, (byte)iConfig, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted)) 115 if (!GetDescriptor((byte)UsbDescriptorType.Configuration, (byte)iConfig, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted))
116 throw new Exception("Could not read configuration descriptor"); 116 throw new Exception("Could not read configuration descriptor");
117 if (iBytesTransmitted < UsbConfigDescriptor.Size || cfgBuffer[1] != (byte)UsbDescriptorType.Configuration) 117 if (iBytesTransmitted < UsbConfigDescriptor.Size || cfgBuffer[1] != (byte)UsbDescriptorType.Configuration)
118 throw new Exception("GetDeviceConfigs: USB config descriptor is invalid."); 118 throw new Exception("GetDeviceConfigs: USB config descriptor is invalid.");
119 UsbConfigDescriptor configDescriptor = new UsbConfigDescriptor(); 119 UsbConfigDescriptor configDescriptor = new UsbConfigDescriptor();
120 Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor); 120 Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor);
121 if (configDescriptor.TotalLength > cfgBuffer.Length) {
122 cfgBuffer = new Byte[configDescriptor.TotalLength];
123 if (!GetDescriptor((byte)UsbDescriptorType.Configuration, (byte)iConfig, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted))
124 throw new Exception("Could not read configuration descriptor");
125 if (iBytesTransmitted < UsbConfigDescriptor.Size || cfgBuffer[1] != (byte)UsbDescriptorType.Configuration)
126 throw new Exception("GetDeviceConfigs: USB config descriptor is invalid.");
127 Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor);
128 }
121 if (configDescriptor.TotalLength != iBytesTransmitted) throw new Exception("GetDeviceConfigs: USB config descriptor length doesn't match the length received."); 129 if (configDescriptor.TotalLength != iBytesTransmitted) throw new Exception("GetDeviceConfigs: USB config descriptor length doesn't match the length received.");
122 List<byte[]> rawDescriptorList = new List<byte[]>(); 130 List<byte[]> rawDescriptorList = new List<byte[]>();
123 int iRawLengthPosition = configDescriptor.Length; 131 int iRawLengthPosition = configDescriptor.Length;
124 while (iRawLengthPosition < configDescriptor.TotalLength) { 132 while (iRawLengthPosition < configDescriptor.TotalLength) {
125 byte[] rawDescriptor = new byte[cfgBuffer[iRawLengthPosition]]; 133 byte[] rawDescriptor = new byte[cfgBuffer[iRawLengthPosition]];