internal static List <UsbConfigInfo> GetDeviceConfigs(UsbDevice usbDevice) { List <UsbConfigInfo> rtnConfigs = new List <UsbConfigInfo>(); byte[] cfgBuffer = new byte[UsbConstants.MAX_CONFIG_SIZE]; int iConfigs = usbDevice.Info.Descriptor.ConfigurationCount; for (int iConfig = 0; iConfig < iConfigs; iConfig++) { int iBytesTransmitted; bool bSuccess = usbDevice.GetDescriptor((byte)DescriptorType.Configuration, 0, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted); if (bSuccess) { if (iBytesTransmitted >= UsbConfigDescriptor.Size && cfgBuffer[1] == (byte)DescriptorType.Configuration) { UsbConfigDescriptor configDescriptor = new UsbConfigDescriptor(); Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor); if (configDescriptor.TotalLength == iBytesTransmitted) { List <byte[]> rawDescriptorList = new List <byte[]>(); int iRawLengthPosition = configDescriptor.Length; while (iRawLengthPosition < configDescriptor.TotalLength) { byte[] rawDescriptor = new byte[cfgBuffer[iRawLengthPosition]]; if (iRawLengthPosition + rawDescriptor.Length > iBytesTransmitted) { throw new UsbException(usbDevice, "Descriptor length is out of range."); } Array.Copy(cfgBuffer, iRawLengthPosition, rawDescriptor, 0, rawDescriptor.Length); rawDescriptorList.Add(rawDescriptor); iRawLengthPosition += rawDescriptor.Length; } rtnConfigs.Add(new UsbConfigInfo(usbDevice, configDescriptor, ref rawDescriptorList)); } else { UsbError.Error(ErrorCode.InvalidConfig, 0, "GetDeviceConfigs: USB config descriptor length doesn't match the length received.", usbDevice); } } else { UsbError.Error(ErrorCode.InvalidConfig, 0, "GetDeviceConfigs: USB config descriptor is invalid.", usbDevice); } } else { UsbError.Error(ErrorCode.InvalidConfig, 0, "GetDeviceConfigs", usbDevice); } } return(rtnConfigs); }
internal UsbConfigInfo(MonoUsbDevice usbDevice, MonoUsbConfigDescriptor configDescriptor) { mUsbDevice = usbDevice; mUsbConfigDescriptor = new UsbConfigDescriptor(configDescriptor); List <MonoUsbInterface> monoUSBInterfaces = configDescriptor.InterfaceList; foreach (MonoUsbInterface usbInterface in monoUSBInterfaces) { List <MonoUsbAltInterfaceDescriptor> monoUSBAltInterfaces = usbInterface.AltInterfaceList; foreach (MonoUsbAltInterfaceDescriptor monoUSBAltInterface in monoUSBAltInterfaces) { UsbInterfaceInfo usbInterfaceInfo = new UsbInterfaceInfo(mUsbDevice, monoUSBAltInterface); mInterfaceList.Add(usbInterfaceInfo); } } }
internal UsbConfigInfo(UsbDevice usbDevice, UsbConfigDescriptor descriptor, ref List <byte[]> rawDescriptors) { mUsbDevice = usbDevice; mUsbConfigDescriptor = descriptor; mRawDescriptors = rawDescriptors; UsbInterfaceInfo currentInterface = null; for (int iRawDescriptor = 0; iRawDescriptor < rawDescriptors.Count; iRawDescriptor++) { byte[] bytesRawDescriptor = rawDescriptors[iRawDescriptor]; switch (bytesRawDescriptor[1]) { case (byte)DescriptorType.Interface: currentInterface = new UsbInterfaceInfo(usbDevice, bytesRawDescriptor); mRawDescriptors.RemoveAt(iRawDescriptor); mInterfaceList.Add(currentInterface); iRawDescriptor--; break; case (byte)DescriptorType.Endpoint: if (currentInterface == null) { throw new UsbException(this, "Recieved and endpoint descriptor before receiving an interface descriptor."); } currentInterface.mEndpointInfo.Add(new UsbEndpointInfo(bytesRawDescriptor)); mRawDescriptors.RemoveAt(iRawDescriptor); iRawDescriptor--; break; default: if (currentInterface != null) { currentInterface.mRawDescriptors.Add(bytesRawDescriptor); mRawDescriptors.RemoveAt(iRawDescriptor); iRawDescriptor--; } break; } } }